import pygame
from pytmx import load_pygame, TiledObjectGroup

from actor import WalkDir
from actor.army_based import Army
from actor.final_boss import Boss
from actor.japanese_army_soldier import JapaneseArmySoldierUp, JapaneseArmySoldierR, JapaneseArmySoldierL
from actor.japanese_soldier import JapaneseWalkSoldierUpAndDown, JapaneseWalkSoldierLAndR
from actor.mine_boom import Mine
from actor.shoot_bullet import Bullet
from actor.things_show_image import Prize, Sword
from actor.xiao_tie import XiaoTie
from game import ScenePassStatus
from scene import BaseScene
from scene.task.stage_one import TaskStatus
from scene.task.stage_two.task1_find_weapon import FindWeapon
from scene.task.stage_two.task2_fight_find import FightAndFind
from scene.task.stage_two.task3_save_army import SaveArmy
from scene.task.stage_two.task4_final_fight import FinalTask

"""
最终场景类
"""


class FinalScene(BaseScene):

    def __init__(self, xiao_tie: XiaoTie):
        """
        参数初始化
        :param xiao_tie:
        """
        super(FinalScene, self).__init__(xiao_tie,
                                         "./resources/tmx/final_scene.png")
        self.sound = pygame.mixer.Sound('resources/sound/最终决战.mp3')
        self.sound.play(-1)
        self.japanese_group = pygame.sprite.Group()
        self.prizes_group = pygame.sprite.Group()
        self.obstacle_group = pygame.sprite.Group()
        self.sword_image_group = pygame.sprite.Group()
        self.boss_group = pygame.sprite.Group()
        self.mines_group = pygame.sprite.Group()
        self.army_one = None
        self.army_two = None
        self.time_stamp = 10
        self.tiled_map_data = load_pygame("resources/tmx/final_scene.tmx")
        mine = pygame.image.load("./resources/images/mine/地雷.png")
        self.mine_image = pygame.transform.scale(mine, (40, 40))
        self.army_image = pygame.image.load("resources/images/chinese/red_army/red_army_one.png")
        self.tasks = []
        self.task_id = 0
        self.init_groups()
        self.init_task()

    def init_task(self):
        """
        任务初始化
        :return:
        """
        # self.tasks.append(FindWeapon(self.xiao_tie, self.japanese_group, self.mines_group, self.font))
        # self.tasks.append(FightAndFind(self.xiao_tie, self.japanese_group, self.mines_group, self.prizes_group, self.font))
        # self.tasks.append(SaveArmy(self.xiao_tie, self.japanese_group,
        #                            self.mines_group, self.prizes_group,
        #                           self.army_one, self.army_two, self.font))
        self.tasks.append(FinalTask(self.xiao_tie, self.japanese_group,
                                    self.mines_group, self.prizes_group,
                                    self.sword_image_group, self.boss_group, self.font,
                                    self.obstacle_group))

    def run(self, key_down_flag, key_list):
        """
        场景角色道具移动
        :param key_down_flag:
        :param key_list:
        :return:
        """
        self.xiao_tie.run(key_down_flag, key_list, self.obstacle_group)

        for japanese in self.japanese_group:
            japanese.run(key_down_flag, key_list, self.obstacle_group)
        self.tasks[self.task_id].do_task(key_down_flag, key_list)
        task_status = self.tasks[self.task_id].get_status()
        # 如果胜利窗口显示完毕
        if task_status == TaskStatus.win_over:
            self.task_id += 1
            # 判断是否大于最大长度，如果是则胜利
            if self.task_id >= len(self.tasks):
                self.pass_status = ScenePassStatus.final_win
                pygame.mixer.music.stop()
        elif task_status == TaskStatus.fail_over:
            self.pass_status = ScenePassStatus.final_fail
            pygame.mixer.music.stop()

    def init_groups(self):
        """
        精灵组初始化
        :return:
        """
        for group in self.tiled_map_data.objectgroups:
            if isinstance(group, TiledObjectGroup):
                if group.name == '日军射击上':
                    for obj in group:
                        japanese = JapaneseArmySoldierUp(obj.x, obj.y, self.xiao_tie)
                        self.japanese_group.add(japanese)
                elif group.name == '日军射击左':
                    for obj in group:
                        japanese = JapaneseArmySoldierL(obj.x, obj.y, self.xiao_tie)
                        self.japanese_group.add(japanese)
                elif group.name == '日军射击右':
                    for obj in group:
                        japanese = JapaneseArmySoldierR(obj.x, obj.y, self.xiao_tie)
                        self.japanese_group.add(japanese)
                elif group.name == '日军上下':
                    for obj in group:
                        japanese = JapaneseWalkSoldierUpAndDown(obj.x, obj.y)
                        self.japanese_group.add(japanese)
                elif group.name == '日军左右':
                    for obj in group:
                        japanese = JapaneseWalkSoldierLAndR(obj.x, obj.y)
                        self.japanese_group.add(japanese)
                elif group.name == '道具':
                    for obj in group:
                        prizes = Prize(obj.x, obj.y)
                        prizes.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.prizes_group.add(prizes)
                elif group.name == '障碍物':
                    for obj in group:
                        obs = pygame.sprite.Sprite()
                        obs.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.obstacle_group.add(obs)
                elif group.name == '小刀':
                    for obj in group:
                        sword = Sword(obj.x, obj.y)
                        self.sword_image_group.add(sword)

                elif group.name == '地雷':
                    for obj in group:
                        mine = Mine(obj.x, obj.y, self.mine_image)
                        mine.rect = pygame.Rect(obj.x, obj.y, obj.width, obj.height)
                        self.mines_group.add(mine)
                elif group.name == 'npc1':
                    for obj in group:
                        self.army_one = Army(obj.x, obj.y, self.army_image)
                elif group.name == 'npc2':
                    for obj in group:
                        self.army_two = Army(obj.x, obj.y, self.army_image)
                elif group.name == '小铁':
                    for obj in group:
                        self.xiao_tie.set_pos(obj.x, obj.y)
                        self.xiao_tie.set_count(0)
                        self.xiao_tie.set_status("army")
                elif group.name == "boss":
                    for obj in group:
                        boss = Boss(obj.x, obj.y, self.xiao_tie)
                        self.boss_group.add(boss)

    def draw_actor(self):
        """
        角色道具绘制
        :return:
        """
        self.xiao_tie.draw(self.current_surface, self.view_x, self.view_y)
        for obj in self.japanese_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        for obj in self.prizes_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        for obj in self.sword_image_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        for obj in self.mines_group:
            obj.draw(self.current_surface, self.view_x, self.view_y)

        self.tasks[self.task_id].draw(self.current_surface, self.view_x, self.view_y)

    def get_pass_status(self):
        """
        获取游戏状态
        :return:
        """
        if self.pass_status == ScenePassStatus.final_win:
            return self.pass_status, "WinScene"
        elif self.pass_status == ScenePassStatus.final_fail:
            return self.pass_status, "FailScene"
        else:
            return self.pass_status, None
